package com.mt.mtyx.product.controller;

import com.mt.mtyx.common.exception.BusinessException;
import com.mt.mtyx.common.result.Result;
import com.mt.mtyx.entity.product.Attr;
import com.mt.mtyx.product.service.AttrService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author WMT
 * @Date 2023/7/20 10:33
 */
@Api(tags = "属性模块")
@RequestMapping("/admin/product/attr")
@RestController
public class AttrController {

    @Autowired
    private AttrService attrService;

    @ApiOperation("根据分组id获取属性列表")
    @GetMapping("/{groupId}")
    public Result<List<Attr>> getAllByGroupId(@PathVariable Long groupId) {
        if (StringUtils.isEmpty(groupId)) {
            throw new BusinessException("平台属性分组id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrService.getAllByGroupId(groupId));
    }

    @ApiOperation("根据id查询属性详情")
    @GetMapping("/get/{id}")
    public Result<Attr> getDetails(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrService.getById(id));
    }

    @ApiOperation("新增属性")
    @PostMapping("/save")
    public Result<Integer> save(@RequestBody Attr attr) {
        if (StringUtils.isEmpty(attr.getAttrGroupId())) {
            throw new BusinessException("平台属性分组id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(attr.getName())) {
            throw new BusinessException("属性名不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrService.insert(attr));
    }

    @ApiOperation("删除属性")
    @DeleteMapping("/remove/{id}")
    public Result<Integer> remove(@PathVariable Long id) {
        if (StringUtils.isEmpty(id)) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrService.delete(id));
    }

    @ApiOperation("批量删除属性")
    @DeleteMapping("/batchRemove")
    public Result<Integer> remove(@RequestBody List<Long> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrService.batchDelete(ids));
    }

    @ApiOperation("修改属性")
    @PostMapping("/update")
    public Result<Integer> update(@RequestBody Attr attr) {
        if (StringUtils.isEmpty(attr.getId())) {
            throw new BusinessException("id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(attr.getAttrGroupId())) {
            throw new BusinessException("平台属性分组id不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        if (StringUtils.isEmpty(attr.getName())) {
            throw new BusinessException("属性名不能为空！", HttpStatus.BAD_REQUEST.value());
        }
        return Result.success(attrService.update(attr));
    }
}
